package swing;

import app.Main;

import packages.db.DBUtil;
import user.UserInfo;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import static app.Main.viewChange;

public class FeedView extends JPanel{
    private JComboBox OptionComboBox;
    private JTextField InfoField;
    private JButton Button2;
    private JButton Button1;
    public JPanel JPanel0;
    private JPanel JPanel1;
    private JPanel JPanel2;
    private JPanel JPanel3;
    private JLabel TitleLabel;
    private JLabel NameLabel;
    private JLabel FoodLabel;
    private JComboBox comboBox1;
    private JComboBox comboBox2;
    private JComboBox comboBox3;
    private JLabel LocationLabel;
    ArrayList<String> name = new ArrayList<>();
    ArrayList<String> food = new ArrayList<>();
    ArrayList<String> location = new ArrayList<>();





    public FeedView() throws IOException {
        Font font=new Font("楷体", Font.BOLD,30);
        TitleLabel.setFont(font);

        font = new Font("楷体", Font.BOLD,20);
        Button1.setFont(font);
        Button2.setFont(font);
        NameLabel.setFont(font);
        FoodLabel.setFont(font);


        Color color = new Color(0xff, 0xff, 0xff);
        Button1.setBackground(color);
        Button2.setBackground(color);

        JPanel0.setOpaque(false);
        JPanel1.setOpaque(false);
        JPanel2.setOpaque(false);
        JPanel3.setOpaque(false);



        DBUtil db = new DBUtil();
        Object[] objs = {};
        ResultSet set = db.select("select * from food", objs);
        try {
            while (set.next()) {
                food.add(set.getString("food"));
            }
        } catch (SQLException sqle) {
            sqle.printStackTrace();
        }

        for(String str : food){
            comboBox1.addItem(str);
        }

        set = db.select("select * from location", objs);
        try {
            while (set.next()) {
                location.add(set.getString("location"));
            }
        } catch (SQLException sqle) {
            sqle.printStackTrace();
        }

        for(String str : location){
            comboBox3.addItem(str);
        }


        set = db.select("select * from cat", objs);
        try {
            while (set.next()) {
                name.add(set.getString("cat_name"));
            }
        } catch (SQLException sqle) {
            sqle.printStackTrace();
        }

        for(String str : name){
            comboBox2.addItem(str);
        }

        db.closeConnection();



        Button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                DBUtil db = new DBUtil();
                UserInfo userInfo = UserInfo.getInstance();
                Object[] obj = {null,
                        comboBox1.getSelectedIndex() + 1,
                        userInfo.getUserId(),
                        comboBox2.getSelectedIndex() + 1,
                        comboBox3.getSelectedIndex() + 1,
                        null
                };
                int res = db.update("insert into feed values(?,?,?,?,?,?)", obj);
                db.closeConnection();

                if(res!=0){
                    JOptionPane.showMessageDialog(null,"投喂记录成功!","提示",
                            JOptionPane.PLAIN_MESSAGE);
                }
                else{
                    JOptionPane.showMessageDialog(null,"投喂记录失败!","提示",
                            JOptionPane.ERROR_MESSAGE);
                }
            }
        });
        Button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                viewChange = 3;
                synchronized (Main.object){
                    Main.object.notify();
                }
            }
        });
    }
}
